import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { appContentUrl } from '../../content.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { content } from '../../content.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { InitService } from '../../../public/serve/init/init.service';
declare var $: any;
@Component({
  selector: 'app-content-product-details',
  templateUrl: './content-product-details.component.html',
  styleUrls: ['./content-product-details.component.css']
})
export class ContentProductDetailsComponent implements OnInit {

  @Output() onSubmit = new EventEmitter<void>();

  details: object = {};
  previewData: any = {};
  fileFormat:any;
  fileContent:any;
  angular:any;

  constructor(
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    private service:Service
  ) { }

  ngOnInit() {
    //已标引的内文主题词 鼠标事件
    let t = this;
    $(document).unbind('mouseover').unbind('mouseout');
    $(document).on('mouseover','.indexing_a',function(e){
        e = e || window.event;
        setTimeout(()=>{
            $(".ng-popver-innertext").toggle(100);
            $(".ng-popver-innertext").css("top",e.pageY + "px");
            $(".ng-popver-innertext").css("left",e.pageX + "px");
        },300);
        let indexing_name = $(this).attr('data-indexing-name');
        $("#ngPopverInnertext li span").html(indexing_name);
    }).on('mouseout','.indexing_a',function(event){
        var div = document.getElementById("ngPopverInnertext");
        if(div){
            var x=event.clientX;
            var y=event.clientY;
            var divx1 = div.offsetLeft;
            var divy1 = div.offsetTop;
            var divx2 = div.offsetLeft + div.offsetWidth;
            var divy2 = div.offsetTop + div.offsetHeight;
            if( x < divx1 || x > divx2 || y < divy1 || y > divy2){
                $(".ng-popver-innertext").hide();
            }
        }
    }).on('mouseout','.ng-popver-innertext',function(event){
        var div = document.getElementById("ngPopverInnertext");
        var x=event.clientX;
        var y=event.clientY;
        var divx1 = div.offsetLeft;
        var divy1 = div.offsetTop;
        var divx2 = div.offsetLeft + div.offsetWidth;
        var divy2 = div.offsetTop + div.offsetHeight;
        if( x < divx1 || x > divx2 || y < divy1 || y > divy2){
            $(".ng-popver-innertext").hide();
        }
    });
  }

  /**
   * 主题词详情
   * @param info
   */
  _details(info: object) {
    this.details = info;
  }

  preview(info: object) {
    this.previewData = info;
    this.fileFormat = info['fileFormat'];
    if (this.fileFormat == 'xml' || this.fileFormat == 'XML') {
      setTimeout(() => {
        //this.fileContent = this._xmlOfHtml(this.previewData.filePath);
        if(this.previewData.idxProductInnertext){
          this.fileContent = this.previewData.idxProductInnertext.fileContent;
        }
        
      }, 100);
      //this.service.openWindowA(this.previewData.filePath)
    }else if(this.fileFormat == 'mp3'){
      setTimeout(() => {
        $("#audio-model").attr('src', this.previewData.filePath);
      }, 100);
    }else if(this.fileFormat == 'mp4'){
      setTimeout(() => {
        $("#video-model").attr('src', this.previewData.filePath);
      }, 100);
    }else if(this.fileFormat == 'pdf'){
      setTimeout(() => {
        $("#pdf-model").find('embed').attr('src', this.previewData.filePath);
      }, 100);
    }
     else {
      setTimeout(() => {
        $("#previews").attr('src', this.previewData.filePath);
      }, 100);
    }
  }

  //获取xml文件中的内容
  _xmlOfHtml(xml_path){
    let content_html = '';

    $.ajax({ 
      type : "POST", 
      url : xml_path, 
      async : false, 
      dataType: 'xml',
      success : function(xml){ 
        if(xml){
          let name = $(xml).find("subclauses").eq(0).attr("name");//获取标题
          
          $(xml).find("titles").each(function () {
            let titles = $(this);
            let newObj = titles[0];
            let p = titles.children("p");

            //获取节点文本
            let p_value = '';
            //标题
            let h = titles.children("h1");
            let h_title = h.text();
            if(!h_title){
              h_title = name;
            }

            let title = '<h1 style="text-align:center">'+h_title+'</h1>';

            p_value = title;
            //正文
            $(p).each(function (i,v) {
              p_value += '<p>'+v.textContent+'</p>';
            });

            content_html = p_value;//newObj.innerHTML;
          });
        }
        
      } 
    }); 

    return content_html;
  }
}
